﻿#include "playscene.h"
#include<QDebug>
#include<QMenuBar>
#include<QPainter>
#include<QTimer>
#include<mypushbutton.h>
#include<QLabel>
#include"mycoin.h"
#include"dataconfig.h"
#include<QPropertyAnimation>
#include<QSound>
#pragma execution_character_set("utf-8");
//PlayScene::PlayScene(QWidget *parent) : QMainWindow(parent)
//{

//}

PlayScene::PlayScene(int levelNum)
{
    QString str = QString("进入了第 %1 关").arg(levelNum);
    qDebug() << str;
    this->levelIndex = levelNum;

    this->setFixedSize(320,588);
    this->setWindowTitle(str);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));

    //用代码实现菜单栏创建
    QMenuBar * menuBar = this->menuBar();
    QMenu * menu = menuBar->addMenu("开始");
    QAction * close = new QAction("关闭");
    menu->addAction(close);
    this->setMenuBar(menuBar);

    //关闭按钮实现功能
    connect(close,&QAction::triggered,[=](){
        this->close();
    });

   QSound * backSound = new QSound(":/res/BackButtonSound.wav");
    //返回按钮
   MyPushButton * backBtn =
            new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
   backBtn->setParent(this);
   backBtn->move(this->width() - backBtn->width(),this->height()-backBtn->height());

   //点击返回
   connect(backBtn,&MyPushButton::clicked,[=](){
       //qDebug() << "你点击了返回按钮";
       backSound->play();
       //延时返回到选择场景中
       QTimer::singleShot(500,this,[=]{
       //告诉选择场景 我返回了 选择场景监听
       emit this->playSceneBack();
       });
   });

   //显示当前的关卡数
   QLabel * label = new QLabel;
   label->setParent(this);
   QFont font;
   font.setFamily("华文新魏");
   font.setPointSize(20);
   QString str1 = QString("Level:%1").arg(levelNum);
   //将字体设置到标签控件中
   label->setFont(font);
   label->setText(str1);
   label->setGeometry(30,this->height()-50,130,50);


   dataConfig config;
   //初始化每个关卡的二维数组
   for(int i=0;i<4;i++)
   {
       for(int j=0;j<4;j++)
       {
           this->gameArray[i][j] = config.mData[this->levelIndex][i][j];
       }
   }

   //胜利图片显示
   QLabel * winLabel = new QLabel;
   QPixmap tmpPix;
   tmpPix.load(":/res/LevelCompletedDialogBg.png");
   winLabel->setGeometry(0,0,tmpPix.width(),tmpPix.height());
   winLabel->setPixmap(tmpPix);
   winLabel->setParent(this);
   winLabel->move((this->width() - tmpPix.width())*0.5,-tmpPix.height());


   //显示金币的背景的图案
   for(int i=0;i<4;i++)
   {
       for(int j=0;j<4;j++)
       {
           QLabel * label = new QLabel;
           label->setGeometry(0,0,50,50);
           label->setPixmap(QPixmap(":/res/BoardNode(1).png"));
           label->move(57+i*50,200+j*50);
           label->setParent(this);

           //创建金币
           QString str;
           if(this->gameArray[i][j] == 1)
           {
               //显示金币
               str = ":/res/Coin0001.png";
           }
           else
           {
               str = ":/res/Coin0008.png";
           }

           QSound * coinSound = new QSound(":/res/ConFlipSound.wav");

           MyCoin * coin = new MyCoin(str);
           coin->setParent(this);
           coin->move(59+i*50,204+j*50);

          //给金币属性赋值
          coin->posX = i;
          coin->posY = j;
          coin->flag = this->gameArray[i][j];//1正面 0反面
          //将金币放入到 金币的二维数组里 以便于后期的维护
          coinBtn[i][j] = coin;

          //点击金币 进行翻转
          connect(coin,&MyCoin::clicked,[=](){

              coinSound->play();

              //翻完周围的金币后，将所有的金币禁用
              for(int i=0;i<4;i++)
              {
                  for(int j=0;j<4;j++)
                  {
                      this->coinBtn[i][j]->isWin = true;
                  }
              }


              coin->changeFlag();
              this->gameArray[i][j] = this->gameArray[i][j] == 0 ? 1 :0;

              //翻转周围硬币，延迟翻转
              QTimer::singleShot(300,this,[=](){
                  if(coin->posX + 1 <= 3)//周围的右侧金币翻转的条件
                  {
                        coinBtn[coin->posX+1][coin->posY]->changeFlag();
                        this->gameArray[coin->posX+1][coin->posY] = this->gameArray[i][j] == 0 ? 1 :0;

                  }
                  if(coin->posX - 1 >= 0)//周围的左侧金币翻转的条件
                  {
                      coinBtn[coin->posX-1][coin->posY]->changeFlag();
                      this->gameArray[coin->posX-1][coin->posY] = this->gameArray[i][j] == 0 ? 1 :0;
                  }
                  if(coin->posY + 1 <= 3)//周围的上面的金币翻转的条件
                  {
                      coinBtn[coin->posX][coin->posY+1]->changeFlag();
                      this->gameArray[coin->posX][coin->posY+1] = this->gameArray[i][j] == 0 ? 1 :0;
                  }
                  if(coin->posY - 1 >= 0)//周围的下面的金币翻转的条件
                  {
                      coinBtn[coin->posX][coin->posY-1]->changeFlag();
                      this->gameArray[coin->posX][coin->posY-1] = this->gameArray[i][j] == 0 ? 1 :0;
                  }


                  //翻完周围的金币后，将所有的金币解开禁用
                  for(int i=0;i<4;i++)
                  {
                      for(int j=0;j<4;j++)
                      {
                          this->coinBtn[i][j]->isWin = false;
                      }
                  }



                  QSound * successSound = new QSound(":/res/LevelWinSound.wav");
                  //判断是否胜利
                  this->isWin = true;
                  for(int i = 0;i<4;i++)
                  {
                      for(int j=0;j<4;j++)
                      {
                            if(coinBtn[i][j]->flag == false)//只要有一个是反面，那就算失败
                            {
                                this->isWin = false;
                                break;
                            }
                      }
                  }

                  if(this->isWin == true)
                  {
                      successSound->play();
                      //胜利了
                      qDebug() << "游戏胜利";
                      //将所有按钮的胜利标志改成true,如果再次点击按钮，直接return，不做响应
                      for(int i = 0;i<4;i++)
                      {
                          for(int j=0;j<4;j++)
                          {
                             coinBtn[i][j]->isWin = true;
                          }
                      }
                      //将胜利的图片移动下来
                      QPropertyAnimation * animation = new QPropertyAnimation(winLabel,"geometry");
                      //设置事件间隔
                      animation->setDuration(1000);
                      //设置开始位置
                      animation->setStartValue(QRect(winLabel->x(),winLabel->y(),winLabel->width(),winLabel->height()));

                      //设置结束位置
                      animation->setEndValue(QRect(winLabel->x(),winLabel->y()+114,winLabel->width(),winLabel->height()));
                      //设置缓和曲线
                      animation->setEasingCurve(QEasingCurve::OutBounce);
                      //执行动画
                      animation->start();
                  }
              });



          });



   }

}
}

void PlayScene::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //画背景上图标
    pix.load(":/res/Title.png");
    //觉得图标太大，缩放图标
    pix = pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(10,30,pix);



}
